import 'package:flutter/material.dart';
import 'package:package_info/package_info.dart';
import 'highscores.dart';
import 'settings.dart';
import 'game.dart';
import 'about.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: '形卡连接',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        fontFamily: 'kuaile',
        appBarTheme: AppBarTheme(
          backgroundColor: Colors.green,
          centerTitle: false,
        ),
        primaryColor: Colors.green,
        brightness: Brightness.dark,
        textTheme: TextTheme(
          headline1: TextStyle(
            fontSize: 20,
            fontWeight: FontWeight.normal,
            height: 2,
            color: Colors.green,
          ),
        ),
        textButtonTheme: TextButtonThemeData(
          style: TextButton.styleFrom(
              visualDensity: VisualDensity.compact,
              backgroundColor: Colors.green,
              primary: Colors.white),
        ),
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key? key}) : super(key: key);
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class GameOption {
  final String title;
  final int cols, rows, depth;

  GameOption(this.title, this.cols, this.rows, this.depth);

  static final options = <GameOption>[
    GameOption("入门", 2, 2, 2),
    GameOption("初级", 2, 2, 3),
    GameOption("简单", 2, 3, 3),
    GameOption("普通", 2, 3, 4),
    GameOption("中级", 2, 3, 5),
    GameOption("困难", 2, 4, 4),
    GameOption("高级", 2, 4, 5),
    GameOption("超难", 3, 4, 4),
    GameOption("专家", 3, 4, 5),
  ];
}

class CardOption {
  final int cols, rows;

  CardOption(this.cols, this.rows);

  get count => cols * rows;
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: FutureBuilder<String>(
            initialData: "",
            future: PackageInfo.fromPlatform().then((a) => a.version),
            builder: (context, value) => Text("形卡连接")),
      ),
      drawer: SettingsDrawer(false),
      body: Center(
        child: IntrinsicWidth(
            child: Column(
                mainAxisSize: MainAxisSize.min,
                crossAxisAlignment: CrossAxisAlignment.stretch,
                children: <Widget>[
              Stack(children: [
                Text("难度选择"),
                Container(
                    padding: EdgeInsets.only(top: 5),
                    child: FutureBuilder(
                        future: settings.loaded.future,
                        builder: (context, _) => Slider(
                              value: settings.difficulty.toDouble(),
                              min: 0,
                              max: GameOption.options.length - 1,
                              divisions: GameOption.options.length - 1,
                              label:
                                  GameOption.options[settings.difficulty].title,
                              onChanged: (v) => setState(
                                  () => settings.difficulty = v.toInt()),
                            )))
              ]),
              TextButton(
                  child: Padding(
                    padding: EdgeInsets.all(10),
                    child: Text(
                      "开始游戏",
                      style: TextStyle(
                        fontSize: 22,
                      ),
                    ),
                  ),
                  onPressed: () =>
                      loadGame(context, settings.difficulty, false)),
              SizedBox(height: 10),
              TextButton(
                  child: Padding(
                    padding: EdgeInsets.all(10),
                    child: Text(
                      "练习模式",
                      style: TextStyle(
                        fontSize: 22,
                      ),
                    ),
                  ),
                  onPressed: () =>
                      loadGame(context, settings.difficulty, true)),
              SizedBox(height: 10),
              TextButton(
                  child: Padding(
                    padding: EdgeInsets.all(10),
                    child: Text(
                      "玩法介绍",
                      style: TextStyle(
                        fontSize: 22,
                      ),
                    ),
                  ),
                  onPressed: () => Navigator.push(context,
                      MaterialPageRoute(builder: (context) => HowToPlay()))),
              // SizedBox(height: 10),
              // TextButton(
              //     child: Padding(
              //       padding: EdgeInsets.all(10),
              //       child: Text(
              //         "分数记录",
              //         style: TextStyle(
              //           fontSize: 22,
              //         ),
              //       ),
              //     ),
              //     onPressed: () => Navigator.push(
              //         context,
              //         MaterialPageRoute(
              //             builder: (context) => HighscoresView()))),
            ])),
      ),
    );
  }

  loadGame(BuildContext context, int level, bool practise) {
    final GameOption g = GameOption.options[level];
    Navigator.push(
        context,
        MaterialPageRoute(
            builder: (context) => SafeArea(
                child: Game(level, g.cols, g.rows, g.depth, practise))));
  }
}
